//
//  PictureBrowserAnimator.swift
//  MicroBlog
//
//  Created by  NorthCity on 15/11/19.
//  Copyright © 2015年  NorthCity. All rights reserved.
//

import UIKit
/// 动画标记
var isPresent:Bool = false
//MARK: - 展现转场协议
protocol PictureBrowserPresentDelegate:NSObjectProtocol{
/// 参与转场动画视全新视图
    func imageViewForPresent(indexPath:NSIndexPath)->UIImageView
/// 转场动画开始位置
    func pictureBrowserFromRect(indexPath:NSIndexPath)->CGRect
/// 转场动画结束位置
    func pictureBrowserToRect(indexPath:NSIndexPath)->CGRect
}
//MARK: - 解除动画协议
protocol PictureBrowserDismissDelegate:NSObjectProtocol{
    func imageViewForDismiss()->UIImageView
    
    func indexPathForDismiss()->NSIndexPath
    
}





class PictureBrowserAnimator: NSObject,UIViewControllerTransitioningDelegate {
/// 展现代理
    weak var presentDelegate:PictureBrowserPresentDelegate?
/// 解除代理
    weak var dismissDelegate:PictureBrowserDismissDelegate?
/// 动画图像索引
    var indexPath:NSIndexPath?
/// 设置代理参数
    func setDelegateParams(presentDelegate:PictureBrowserPresentDelegate,dismissDelegate:PictureBrowserDismissDelegate,indexPath:NSIndexPath){
        self.presentDelegate = presentDelegate
        self.dismissDelegate = dismissDelegate
        self.indexPath = indexPath
    }
    
/// modal 展现动画对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
/// dismiss 关闭动画对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
}
//MARK: -  实现转场动画实现
extension PictureBrowserAnimator:UIViewControllerAnimatedTransitioning{
    /// 动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    /// 动画实现方法,一旦实现此方法, 系统动画不执行
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

        isPresent ? presentAnimation(transitionContext):dismissAnimation(transitionContext)

    }
    /// 展现转场动画
    private func presentAnimation(transitionContext: UIViewControllerContextTransitioning){
//        guard let 会把属性变成局部变量，后续的闭包中不需要 self，也不需要考虑解包
        guard let presentDelegate = presentDelegate, indexPath = indexPath else{
            return
        }
//        1 真正展现的视图
//        1> 获取视图
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
//        2> 视图添加到容器视图
        transitionContext.containerView()?.addSubview(toView)
//        3> 设置透明度
        toView.alpha = 0.0
//        2 动画虚假视图
//        1> 建立转场动画图像视图
        let iv = presentDelegate.imageViewForPresent(indexPath)
//        2> 初始位置
        iv.frame = presentDelegate.pictureBrowserFromRect(indexPath)
//        3> 添加到容器
        transitionContext.containerView()?.addSubview(iv)
        
        UIView.animateWithDuration(
            transitionDuration(transitionContext),
            animations: {
                iv.frame = presentDelegate.pictureBrowserToRect(indexPath)
                iv.alpha = 0
              toView.alpha = 1.0
            }){ _ in
                iv.removeFromSuperview()
                transitionContext.completeTransition(true)
              }
    }
   ///解除转场动画
    private func dismissAnimation(transitionContext: UIViewControllerContextTransitioning){
        guard let presentDelegate = presentDelegate,dismissDelegate = dismissDelegate else{
            return
        }
        
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!

        
//        动画视图
        let iv = dismissDelegate.imageViewForDismiss()
        let indexPath = dismissDelegate.indexPathForDismiss()
        transitionContext.containerView()?.addSubview(iv)
        UIView.animateWithDuration(
            transitionDuration(transitionContext),
            animations: {
          fromView.alpha = 0.0
          iv.frame = presentDelegate.pictureBrowserFromRect(indexPath)
                
            }){ _ in
                iv.removeFromSuperview()
               fromView.removeFromSuperview()
               transitionContext.completeTransition(true)
        }
        
    }
}
